﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VAdvantage.Classes;
using VAdvantage.Common;
//using ViennaAdvantage.Process;
//using System.Windows.Forms;
//using ViennaAdvantage.Model;
using VAdvantage.DataBase;
using VAdvantage.SqlExec;
using VAdvantage.Utility;
using System.Data;
using VAdvantage.Logging;
//using VAdvantage.Controller;
using VAdvantage.Model;
using VAdvantage.Grids;


namespace VAdvantage.Model
{
    public class CalloutTeamForcast : CalloutEngine
    {

        public String ProductInfo(Context ctx, int WindowNo, GridTab mTab, GridField mField, Object value)
        {
            if (value == DBNull.Value || value == null || value.ToString() == "" || Util.GetValueOfInt(value) == 0)
            {
                return "";
            }

            int M_Product_ID = System.Convert.ToInt32(value);
            if (M_Product_ID == null || M_Product_ID == 0)
                return "";
            int M_PriceList_ID = ctx.GetContextAsInt(WindowNo, "M_PriceList_ID");
            if (M_PriceList_ID != 0)
            {
                String query = "Select M_PriceList_Version_ID from M_ProductPrice where M_Product_id=" + Util.GetValueOfInt(value) +
                                " and M_PriceList_Version_ID in (select m_pricelist_version_id from m_pricelist_version" +
                              " where m_pricelist_id = " + M_PriceList_ID + " and isactive='Y')";
                int M_PriceList_Version_ID = Util.GetValueOfInt(DB.ExecuteScalar(query, null, null));
                if (M_PriceList_Version_ID != 0)
                {
                    query = "Select PriceStd from M_ProductPrice where M_PriceList_Version_ID=" + M_PriceList_Version_ID + " and M_Product_id=" + Util.GetValueOfInt(value);
                    decimal PriceStd = Util.GetValueOfDecimal(DB.ExecuteScalar(query, null, null));
                    //ForcastLine.SetPriceStd(PriceStd);
                    mTab.SetValue("PriceStd", PriceStd);
                    mTab.SetValue("UnitPrice", PriceStd);
                    mTab.SetValue("PriceStd", Decimal.Multiply(PriceStd,Util.GetValueOfDecimal(mTab.GetValue("QtyEntered"))));
                }
            }
            else
            {
                ShowMessage.Info("PriceLisetNotFound", true, null, null);
            }


            //ViennaAdvantage.Model.X_C_ForecastLine ForcastLine = new ViennaAdvantage.Model.X_C_ForecastLine(ctx, Util.GetValueOfInt(mTab.GetRecord_ID()), null);

            //  ViennaAdvantage.Model.X_M_Product product = new ViennaAdvantage.Model.X_M_Product(ctx, Util.GetValueOfInt(value), null);
            //  //String query = "Select M_PriceList_Version_ID from M_ProductPrice where M_Product_id=" + Util.GetValueOfInt(value);
            //  String query = "Select M_PriceList_Version_ID from M_ProductPrice where M_Product_id=" + Util.GetValueOfInt(value) +
            //                  " and M_PriceList_Version_ID in (select m_pricelist_version_id from m_pricelist_version"+
            //                " where m_pricelist_id in (select m_pricelist_id from m_pricelist where isdefault='Y'"+ 
            //                  " and issopricelist='Y')and isactive='Y')";
            //  int versionID = Util.GetValueOfInt(DB.ExecuteScalar(query,null,null));

            //// ViennaAdvantage.Model.X_M_ProductPrice ProPrice = new ViennaAdvantage.Model.X_M_ProductPrice(ctx, priceID, null);
            //  mTab.SetValue("C_UOM_ID", product.GetC_UOM_ID());
            //  //ForcastLine.SetC_UOM_ID(product.GetC_UOM_ID());
            //  mTab.SetValue("QtyEntered", 1.0);
            // // ForcastLine.SetQtyEntered(1);
            //  query = "Select PriceStd from M_ProductPrice where M_PriceList_Version_ID=" + versionID + " and M_Product_id=" + Util.GetValueOfInt(value);
            //  decimal PriceStd = Util.GetValueOfDecimal(DB.ExecuteScalar(query, null, null));
            //  //ForcastLine.SetPriceStd(PriceStd);
            //  mTab.SetValue("PriceStd", PriceStd);
            //  mTab.SetValue("UnitPrice", PriceStd);

            return "";
        }

        public String CalculatePrice(Context ctx, int WindowNo, GridTab mTab, GridField mField, Object value)
        {
            if (value == DBNull.Value || value == null || value.ToString() == "" || Util.GetValueOfInt(value) <= 0)
            {
                return "";
            }

            //String query = "Select M_PriceList_Version_ID from M_ProductPrice where M_Product_id=" + mTab.GetValue("M_Product_ID") +
            //               " and M_PriceList_Version_ID in (select m_pricelist_version_id from m_pricelist_version" +
            //             " where m_pricelist_id in (select m_pricelist_id from m_pricelist where isdefault='Y'" +
            //               " and issopricelist='Y')and isactive='Y')";
            //int versionID = Util.GetValueOfInt(DB.ExecuteScalar(query, null, null));
            //query = "Select PriceStd from M_ProductPrice where M_PriceList_Version_ID=" + versionID + " and M_Product_id=" + mTab.GetValue("M_Product_ID");
            //decimal PriceStd = Util.GetValueOfDecimal(DB.ExecuteScalar(query, null, null));
            // ViennaAdvantage.Model.X_C_ForecastLine ForcastLine = new ViennaAdvantage.Model.X_C_ForecastLine(ctx, Util.GetValueOfInt(mTab.GetAD_Table_ID()), null);
            decimal price = Util.GetValueOfDecimal(mTab.GetValue("UnitPrice")) * Util.GetValueOfDecimal(mTab.GetValue("QtyEntered"));
            // ForcastLine.SetQtyEntered(price);
            mTab.SetValue("PriceStd", price);
            return "";
        }





    }
}